package org.business.accounting.pages;

import java.util.Collection;
import java.util.Vector;

import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.corelib.components.ActionLink;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.business.accounting.beans.CartBean;
import org.business.accounting.beans.CartConfirmHelperBean;
import org.business.accounting.beans.LogOutHelperBean;
import org.business.accounting.components.PreviewOrder;
import org.business.accounting.interfaces.BasicOrder;
import org.business.accounting.interfaces.Cart;
import org.business.accounting.interfaces.CartConfirmHelper;
import org.business.accounting.interfaces.Computer;
import org.business.accounting.interfaces.LogOutHelper;
import org.business.accounting.interfaces.User;
import org.business.accounting.service.ComputerDAO;
import org.business.accounting.service.UserDAO;

@SuppressWarnings("unused")
public class ShowCart {
	
	@ApplicationState
	@Property
	private User user;
	
	@Property
	private boolean userExists;
	
	@ApplicationState
	@Property
	private Cart cart;
	
	private boolean cartExists;
	
	@Inject
	private UserDAO userDAO;
	
	@Inject
	private ComputerDAO computerDAO;
	
	@Inject
	private Messages messages;
	
	@Persist
	@Property
	private LogOutHelper logOutHelper;
	
	//
	// Show Cart
	//
	
	@Property
	private String add = "computer";
	
	@Component(parameters = {
			"source = orders"
	})
	private PreviewOrder previewOrder;
	
	@Property
	private BasicOrder basicOrder;
	
	//
	// Confirm and Cancel
	//
	
	@Component
	private ActionLink confirm;
	
	@Component 
	private ActionLink cancel;
	
	@Component
	private Form ordersForm;
	
	//
	// Geters and Seters
	//
	
	public boolean isAdmin() {
		if (userExists) {
			return user.isAdmin();
		}
		return false;
	}
	
	public Collection<User> getUsers() {
		return userDAO.retrieve();
	}
	
	public Collection<BasicOrder> getOrders() {
		if (cartExists) {
			return cart.getBasicOrders();
		}
		Collection<BasicOrder> collection = new Vector<BasicOrder>();
		return collection;
	}
	
	public Collection<Computer> getComputers() {
		return computerDAO.retrieve();
	}
	
	//
	// Event handlers
	//
	
	void onActionFromCancel() {
		cart.clear();
	}
	
	Object onActionFromConfirm () {
		
		user.setSpentMoney(user.getSpentMoney() + cart.calculatePrice());
		CartConfirmHelper cartConfirmHelper = new CartConfirmHelperBean();
		cartConfirmHelper.setCart(cart);
		cartConfirmHelper.setComputerDAO(computerDAO);
		if ( !cartConfirmHelper.confirm(user.isAdmin()) ) {
			ordersForm.recordError("Amount overflow. Please clear cart and try again.");
			return null;
		}
		
		cart.clear();
		
		return Index.class;
	}
	
	@SetupRender
	void onSetupRender() {
		if (!cartExists) {
			cart = new CartBean();
		}
		
		logOutHelper = new LogOutHelperBean();
		logOutHelper.setCart(cart);
		if (userExists) {
			logOutHelper.setUser(user);
		}
	}

}
